package com.smxy.mall.service.impl;

import com.smxy.mall.common.CustomException;
import com.smxy.mall.common.Response;
import com.smxy.mall.dao.CarDao;
import com.smxy.mall.dao.GoodsDao;
import com.smxy.mall.dao.ShopDao;
import com.smxy.mall.entity.Car;
import com.smxy.mall.entity.Goods;
import com.smxy.mall.model.Current;
import com.smxy.mall.model.request.car.CarDelReq;
import com.smxy.mall.model.request.car.CarRemoveReq;
import com.smxy.mall.model.request.car.CarReq;
import com.smxy.mall.model.response.CarNumRes;
import com.smxy.mall.model.response.CarResponse;
import com.smxy.mall.service.CarService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CarServiceImpl implements CarService {
    @Resource
    private CarDao carDao;
    @Resource
    private GoodsDao goodsDao;
    @Resource
    private ShopDao shopDao;

    /**
     * 添加购物车
     * @param carReq
     * @param current
     * @return
     */
    @Override
    public Car save(CarReq carReq,Current current) {
        Car car = new Car();
        Goods goods = goodsDao.findById(carReq.getGoodsId());
        if(goods==null){
            throw new CustomException(Response.fail("商品不存在"));
        }else if(goods.getNum()<carReq.getNum()){
            throw new CustomException(Response.fail("商品库存不足"));
        }else if(shopDao.findById(carReq.getShopId())==null){
            throw new CustomException(Response.fail("店铺信息不存在"));
        }
        car.setGoodsId(carReq.getGoodsId());
        car.setShopId(carReq.getShopId());
        car.setUserId(current.getUserId());
        // 判断该商品在购物车是否存在，存在数量增加
        Car obj = carDao.findByGoodsIdAndUserIdAndShopId(carReq.getGoodsId(),current.getUserId(),carReq.getShopId());
        if(obj!=null){
            car.setId(obj.getId());
            car.setNum(obj.getNum()+carReq.getNum());
        }else{
            car.setNum(carReq.getNum());
        }
        return carDao.save(car);
    }

    @Override
    public Car remove(CarRemoveReq carRemoveReq, Current current) {
        Car car = new Car();
        car.setGoodsId(carRemoveReq.getGoodsId());
        car.setShopId(carRemoveReq.getShopId());
        car.setUserId(current.getUserId());
        // 判断该商品在购物车是否存在
        Car obj = carDao.findByGoodsIdAndUserIdAndShopId(carRemoveReq.getGoodsId(),current.getUserId(),carRemoveReq.getShopId());
        if(obj==null){
            throw new CustomException(Response.fail("购物车中暂无该商品信息"));
        }
        if(obj.getNum()>1){
            car.setId(obj.getId());
            car.setNum(obj.getNum()-1);
            return carDao.save(car);
        }else{
            try {
                carDao.deleteById(obj.getId());
                return carDao.findById(obj.getId());
            } catch (Exception e){
                throw new CustomException(Response.fail("删除失败"));
            }
        }
    }

    @Override
    public Integer delete(CarDelReq carDelReq, Current current) {
        List<Car> list = carDao.findByUserIdAndShopId(current.getUserId(),carDelReq.getShopId());
        if(list.size()>0){
            return carDao.deleteAllByUserIdAndShopId(current.getUserId(),carDelReq.getShopId());
        }else{
            throw new CustomException(Response.fail("购物车中暂无数据"));
        }
    }

    @Override
    public List<Car> findByUserId(Current current) {
        return carDao.findByUserId(current.getUserId());
    }

    @Override
    public List<CarResponse> carList(Integer id,Integer shopId) {
        return carDao.carList(id,shopId);
    }

    @Override
    public List<CarNumRes> getGoodsNum(Current current) {
        return carDao.carNumList(current.getUserId());
    }
}
